package com.zyq.validation.validator;

import com.zyq.validation.pojo.entity.User1;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

public class AgeBetweenValidator implements Validator{
    public boolean supports(Class<?> aClass) {//参数是否是User1的字节码对象
        return User1.class.equals(aClass);//判断参数aClass是否为User1.class
    }

    public void validate(Object objTarget, Errors errors) {
        ValidationUtils.rejectIfEmpty(errors,"age","age can not empty年龄不能为空");
        User1 u1=(User1)objTarget;
        if(0>=u1.getAge()){
            errors.rejectValue("age","age<=0, 年龄不能小于等于0");
        }else if( u1.getAge() >=239){
            errors.rejectValue("age","age>=239, 年龄不能大于等于239");
        }
    }
}